# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/24 13:15
desc: 公司详情页
'''
from appium.webdriver.common.appiumby import AppiumBy
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage


class CompanyDetailPage(CLUEAppBasePage):
    _follow_btn_locator_android = (AppiumBy.XPATH, "//*[@resource-id='focus']")
    _follow_btn_locator_ios = (AppiumBy.ACCESSIBILITY_ID, "关注")
    #_follow_btn_locator = (AppiumBy.XPATH,"//*[@text='关注']")
    #_followed_btn_locator = (AppiumBy.XPATH, "//*[contains(@text,'pages/enterprise/detail')]//*[@text='已关注']/..")
    _followed_btn_locator_android = (AppiumBy.XPATH,"//*[@resource-id='focus']")
    _followed_btn_locator_ios = (AppiumBy.ACCESSIBILITY_ID, "已关注")

    #_followed_btn_locator = (AppiumBy.XPATH,"//*[@text='已关注']")
    _potential_customer_locator_android = (AppiumBy.XPATH, "//*[@resource-id='potential']")
    _potential_customer_locator_ios = (AppiumBy.IOS_PREDICATE, 'label contains "潜客"')

    _confirm_btn_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("确认")')
    _confirm_btn_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "确认"')

    #_follow_toast_locator = (AppiumBy.XPATH, "//*[contains(@text, '关注成功')]")
    #_follow_toast_locator = (AppiumBy.XPATH, "//*[@text='关注成功']")
    _follow_toast_locator = (AppiumBy.XPATH, "//*[@class='android.widget.Toast']")

    _back_btn_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "return"')

    def follow_company(self,group_name='默认分组'):
        """

        :param group_name: 关注分组名

        :return:
        """
        self.find_element_and_click(getattr(self, '_follow_btn_locator_' + self.os))
        self.choose_group(group_name)
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))

        #assert "关注成功" in self.driver.find_element(AppiumBy.XPATH,"//*[@class='android.widget.Toast']").text
        # self.driver.implicitly_wait(0)
        # assert "关注成功" in self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text, '关注成功')]").text




    def choose_group(self, group_name):
        if self.os == 'android':
            self.find_element_and_click((AppiumBy.ANDROID_UIAUTOMATOR,
            'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("%s"));'%group_name))
        else:
            # ios中分组弹窗无法获取显示范围，没有办法判断分组是否在显示范围内，暂时只能勾选前三个
            self.find_element_and_click((AppiumBy.IOS_PREDICATE, f'label == "{group_name}"'))

    def get_group_locator(self, group_name):
        #Todo：分组多时需要滑动
        return (AppiumBy.XPATH,"//android.view.View[@text='%s']"%group_name)


    def unfollow_company(self):
        """
        取消关注
        """
        self.find_element_and_click(getattr(self, '_followed_btn_locator_' + self.os))
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))

    def add_potential_customer(self):
        """
        添加潜客
        """
        self.find_element_and_click(getattr(self, '_potential_customer_locator_' + self.os))

    def delete_potential_customer(self):
        """
        删除潜客
        """
        self.find_element_and_click(getattr(self, '_potential_customer_locator_' + self.os))
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))


    def get_follow_button_rect(self):
        """ 获取关注按钮的rect区域信息，用于兼容性测试。主要关注页面布局，因此获取不到关注按钮时，尝试获取“已关注”按钮

        :return:
        """
        try:
            button = self.find_element(getattr(self, '_follow_btn_locator_' + self.os))
        except:
            button = self.find_element(getattr(self, '_followed_btn_locator_' + self.os))

        return button.rect

    def is_company_followed(self):
        """

        :return:
        """

        try:
            self.find_element(getattr(self, '_followed_btn_locator_' + self.os))
            return True
        except Exception:
            return False



    def get_follow_toast_text(self):
        def found(driver):

            if self.driver.find_element(*self._follow_toast_locator):
                return True
            else:
                return False

        self.driver.implicitly_wait(0)
        try:
            #toast = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(self._follow_toast_locator))
            #toast_text = WebDriverWait(self.driver, 5).until(found).get_attribute('text')
            #screenshot_by_element(self.driver,toast,'toast.png')
            toast_text = self.driver.find_element(*self._follow_toast_locator).text
            return toast_text
        except Exception as e:
            print("error: 获取toast失败！")
        #return self.driver.find_element(self._follow_toast_locator).text

    def get_company_detail_name(self):
        """获取公司详情页的公司名称，用于判断是否正确进入详情页面"""
        if self.os == 'android':
            locator = (AppiumBy.XPATH, "//*[contains(@text,'pages/enterprise/detail')]/*[1]/*[1]/*[1]/*[2]/*[1]")
        else:
            locator = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeOther[`label contains "pages/enterprise/detail"`]/XCUIElementTypeStaticText[2]')
        return self.find_element(locator).get_attribute("name")

    def go_back(self):
        """IOS端driver.back()不work，需要使用后退按钮

        :return:
        """
        if self.os == 'android':
            self.driver.back()
        else:
            self.find_element_and_click(getattr(self, '_back_btn_locator_' + self.os))
